package com.mysiteforme.admin.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mysiteforme.admin.entity.ElectricityHeadlines;
import com.mysiteforme.admin.dao.ElectricityHeadlinesDao;
import com.mysiteforme.admin.entity.Role;
import com.mysiteforme.admin.service.ElectricityHeadlinesService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 平台头条 服务实现类
 * </p>
 *
 * @author wangl
 * @since 2019-09-20
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ElectricityHeadlinesServiceImpl extends ServiceImpl<ElectricityHeadlinesDao, ElectricityHeadlines> implements ElectricityHeadlinesService {
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    @Override
    public ElectricityHeadlines save(ElectricityHeadlines electricityHeadlines) {
        baseMapper.insert(electricityHeadlines);
        baseMapper.saveHeadlinesClassify(electricityHeadlines.getId(),electricityHeadlines.getClassifySet());
        return electricityHeadlines;
    }

    @Override
    public ElectricityHeadlines getById(Long id) {
        ElectricityHeadlines electricityHeadlines = baseMapper.selectHeadlinesById(id);
        return electricityHeadlines;
    }

    @Override
    public Integer getCount(String title) {
        EntityWrapper<ElectricityHeadlines> wrapper = new EntityWrapper<>();
        wrapper.eq("title",title);
        return baseMapper.selectCount(wrapper);
    }

    @Transactional(readOnly = false, rollbackFor = Exception.class)
    @Override
    public void updateHeadlines(ElectricityHeadlines electricityHeadlines) {
        baseMapper.updateById(electricityHeadlines);
        baseMapper.dropHeadlinesClassify(electricityHeadlines.getId());
        if(electricityHeadlines.getClassifySet()!=null && electricityHeadlines.getClassifySet().size()>0)
            baseMapper.saveHeadlinesClassify(electricityHeadlines.getId(),electricityHeadlines.getClassifySet());
    }
}
